
   // Fading LED 
// by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 

int value = 0;                            // variable to keep the actual value 
int potPin=2;
int capacity = 800;
int data[800];
int current =-1;
int last = -1;
int size =0;
int numz =0;
int val;
int ledPin = 13;                 // LED connected to digital pin 13

void setup() 
{ 
  Serial.begin(9600);
  // nothing for setup a
  pinMode(ledPin, OUTPUT);  
} 
 
void loop() 
{ 
   val = analogRead(potPin);
   
   //detect new signal
   if( val !=0)
   { 
     int startTime = millis();
     size=1;
     numz=0;
    while(true)
   {
     
     digitalWrite(ledPin, HIGH);   // sets the LED on

    if(size >= capacity)
      {
        break;
      }
      current = analogRead(potPin);
      
      if( current == 0 && last != 0)
      {
        numz=1;
      }
      else if( current ==0 && last ==0)
      {
        numz++; 
        if(numz > 200)
        break;
      }
      
      last = current;
      data[size++] = current;
      delayMicroseconds(1000);
   }
   Serial.print("Time:");
   Serial.println( millis() - startTime );
      Serial.print("millis:");
   Serial.println( millis() );
      Serial.print("startTime:");
   Serial.println( startTime );
   digitalWrite(ledPin, LOW);   // sets the LED on
   for(int i=0; i< size; i++)
   {
 //   Serial.println(data[i]); 
   }
   Serial.print("zeros:");
   Serial.println(numz);
   

  // delay(3000);
   
   }



  
} 
